package com.acme.bankapp.service.bank;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

public class BlockingQueue<T> implements Serializable {
    private final List<T> queue;
    private boolean closed = false;

    public BlockingQueue() {
        this.queue = new LinkedList<T>();
    }

    public void add(T t) {
        if (!closed) {
            synchronized (queue) {
                queue.add(t);
                queue.notify();
            }
        }
    }

    public T get() throws InterruptedException {
        synchronized (queue) {
            while (queue.isEmpty()) {
                if (closed) {
                    return null;
                }
                queue.wait();
            }
            return queue.remove(0);
        }
    }

    public void close() {
        closed = true;
        synchronized (queue) {
            queue.notifyAll();
        }
    }
}
